//Cracking coding interviews 18.1
//add two numbers without + or other arithmetic operations
//5:45PM--5:58PM--6:14PM(bug free)


#include<iostream>
using namespace std;


int add(int a, int b) {
	int c=1;
	for (int i=0; i<8*sizeof(int); i++) {
		if(c&b) {
			if(!(a&c)) {		///< note that '==' priority is higher than '&'
				a=a|c;
			} else {
				a=a&(~c);
				int d=c<<1;
				while(a&d) {
					a=a&(~d);
					d=d<<1;
				}
				a=a|d;
			}
		}
		c=c<<1;
	}
	return a;
}

int addAnswer(int a, int b) {
	if(!b) return a;
	int c=a^b;
	int d=(a&b)<<1;
	return addAnswer(c,d);
}

int main(int argc, char *argv[]) {
	int a=-3478;
	int b=-81;

	cout<<add(a,b)<<"\t"<<a+b<<"\t"<<addAnswer(a, b);

	return 0;
}